import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';

class TextChatItemStyle extends InheritedWidget {
  final Color backgroundColor;
  final Alignment archerAlign;
  final Color textColor;

  TextChatItemStyle(
      {this.backgroundColor, this.textColor, this.archerAlign, Widget child})
      : super(child: child);

  TextChatItemStyle.left({Widget child})
      : backgroundColor = Colors.white,
        archerAlign = Alignment.topLeft,
        textColor = aceBlackColor,
        super(child: child);

  TextChatItemStyle.right({Widget child})
      : backgroundColor = aceBlueColor,
        archerAlign = Alignment.bottomRight,
        textColor = Colors.white,
        super(child: child);

  static TextChatItemStyle of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<TextChatItemStyle>();
  }

  @override
  bool updateShouldNotify(covariant TextChatItemStyle oldWidget) {
    return this.backgroundColor != oldWidget.backgroundColor;
  }
}

class TextChatItem extends StatelessWidget {
  final bool isTop;
  final String content;
  final dynamic actions;
  final ValueChanged<dynamic> onAction;

  const TextChatItem(
      {Key key, this.isTop = false, this.content, this.actions, this.onAction})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final style = TextChatItemStyle.of(context);
    List<Widget> actionWidgets = [];
    if (actions != null && actions is List) {
      for (var i = 0; i < actions.length; i++) {
        final action = actions[i];
        actionWidgets.add(PressedOpacityButton(
          onPressed: () {
            onAction?.call(action);
          },
          padding: EdgeInsets.symmetric(
            vertical: getDp(10),
          ),
          child: Text(
            action['text'],
            textAlign: TextAlign.center,
            style: TextStyle(
              color: aceBlueColor,
              fontSize: getSp(14),
              fontWeight: FontWeight.w400,
              height: 22 / 14,
            ),
          ),
        ));
      }
    } else if (actions != null && actions is Map) {
      actionWidgets.add(PressedOpacityButton(
        onPressed: () {
          onAction?.call(actions);
        },
        padding: EdgeInsets.symmetric(
          vertical: getDp(10),
        ),
        child: Text(
          actions['text'],
          style: TextStyle(
            color: aceBlueColor,
            fontSize: getSp(14),
            fontWeight: FontWeight.w400,
            height: 22 / 14,
          ),
        ),
      ));
    }
    return Container(
      margin: EdgeInsets.only(
          top: isTop == false ? getDp(10) : 0.0, left: getDp(13)),
      constraints: BoxConstraints(
        maxWidth: getDp(249),
      ),
      padding: EdgeInsets.only(
        left: getDp(10),
        right: getDp(10),
        top: getDp(8),
        bottom: getDp(12),
      ),
      decoration: BoxDecoration(
          color: style?.backgroundColor ?? Colors.white,
          borderRadius: BorderRadius.only(
            bottomRight:
                style?.archerAlign == Alignment.bottomRight && isTop == true
                    ? Radius.circular(0.0)
                    : Radius.circular(16),
            bottomLeft: Radius.circular(16),
            topRight: Radius.circular(16),
            topLeft: style?.archerAlign == Alignment.topLeft && isTop == true
                ? Radius.circular(0.0)
                : Radius.circular(16),
          )),
      child: Column(
        children: [
          HtmlWidget(
            content ?? '',
            textStyle: TextStyle(
              color: style?.textColor ?? aceBlackColor,
              fontSize: getSp(16),
              fontWeight: FontWeight.w400,
              height: 22 / 16,
            ),
          ),
          ...actionWidgets,
        ],
      ),
    );
  }
}
